<template>
  <div>
    <div class="top">
      <div class="name">{{ item.name }}</div>
      <div class="manager">{{ item.manager }}</div>
      <el-dropdown @command="commandEvent">
        <div class="other">
          <span>操作</span>
          <i class="el-icon-arrow-down el-icon--right" />
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="add">添加子部门</el-dropdown-item>
            <el-dropdown-item
              v-if="!istop"
              command="look"
            >查看部门</el-dropdown-item>
            <el-dropdown-item
              v-if="!istop"
              command="del"
            >删除部门</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
import { sysRoleDelete } from '@/api/setting.js'
export default {
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    },
    istop: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {}
  },
  methods: {
    commandEvent(command) {
      switch (command) {
        case 'add':
          this.$bus.$emit('addEvent', true, this.item)
          break

        case 'look':
          this.$bus.$emit('addEvent', true, this.item, 'edit')
          break

        case 'del':
          this.$confirm(`你确定删除[${this.item.name}]部门吗?`, '提示')
            .then(async() => {
              await sysRoleDelete(this.item.id)
              this.$message.success('删除成功')
              this.$emit('getData')
            })
            .catch(() => {})
          break
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.top {
  display: flex;
  .name {
    flex: 1;
  }
  .manager {
    width: 80px;
  }
}
</style>
